﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.IO;

namespace VidoopSecure
{
    public class CategoryImage
    {
        private string _uri;
        private string _xml;

        /// <summary>
        /// Uri of this representative image.
        /// </summary>
        public string Uri
        {
            get { return _uri; }
            set { _uri = value; }
        }

        /// <summary>
        /// Raw XML string from which this representative image was created.
        /// </summary>
        public string Xml
        {
            get { return _xml; }
            set { _xml = value; }
        }

        /// <summary>
        /// Determines whether <paramref name="obj"/> is equal to the 
        /// current object.
        /// </summary>
        /// <param name="obj">The <see cref="System.Object" /> to compare against.</param>
        /// <returns>true if the specified object is equal to the current object.</returns>
        public override bool Equals(object obj)
        {
            CategoryImage other = obj as CategoryImage;
            if (other == null)
                return false;

            return(other.Uri == this.Uri && other.Xml == this.Xml);
        }

        /// <summary>
        /// Serves as a hashcode for the type.
        /// </summary>
        /// <returns>An integer representing a unique hashcode for the object.</returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// Generate a <see cref="VidoopSecure.CategoryImage" /> from 
        /// <paramref name="xml"/>.
        /// </summary>
        /// <param name="xml">An XML representation of a 
        /// <see cref="VidoopSecure.CategoryImage" /></param>
        /// <returns>A  <see cref="VidoopSecure.CategoryImage" /> instance.</returns>
        static public CategoryImage FromXml(string xml)
        {
            CategoryImage image = new CategoryImage();
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            image.Uri = doc.GetElementsByTagName("imageURI")[0].InnerText;
            return image;
        }


        /// <summary>
        /// Generate a list of <see cref="VidoopSecure.CategoryImage" />s from 
        /// <paramref name="xml"/>.
        /// </summary>
        /// <param name="xml">An XML representation of a list of 
        /// <see cref="VidoopSecure.CategoryImage" />s.</param>
        /// <returns>A  <see cref="VidoopSecure.CategoryImage" /> instance.</returns>
        static public List<CategoryImage> FromImagesXml(string xml)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            return GenerateCategoryListFromXmlDocument(doc);
        }


        /// <summary>
        /// Generate a list of <see cref="VidoopSecure.CategoryImage" />s from 
        /// <paramref name="stream"/>.
        /// </summary>
        /// <param name="stream">A <see cref="System.IO.Stream" /> representing
        /// the XML of a list of <see cref="VidoopSecure.CategoryImage" />s.
        /// </param>
        /// <returns>A  list of <see cref="VidoopSecure.CategoryImage" /> instances.
        /// </returns>
        static public List<CategoryImage> FromImagesXml(Stream stream)
        {

            XmlDocument doc = new XmlDocument();
            doc.Load(stream);
            return GenerateCategoryListFromXmlDocument(doc);
        }

        static private List<CategoryImage> GenerateCategoryListFromXmlDocument(XmlDocument doc)
        {
            List<CategoryImage> images = new List<CategoryImage>();

            IEnumerator enumerator = doc.GetEnumerator();
            while(enumerator.MoveNext())
            {
                XmlNode node = (XmlNode)enumerator.Current;
                images.Add(CategoryImage.FromXml(node.OuterXml));
            }

            return images;
        }
    }
}
